package com.shangting.web.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shangting.common.result.Result;
import com.shangting.common.entity.UserInfo;
import com.shangting.common.enums.BaseStatus;
import com.shangting.web.service.UserInfoService;
import com.shangting.web.vo.user.UserInfoQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@Tag(name = "用户信息管理")
@RestController
@RequestMapping("/admin/user")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @Operation(summary = "分页查询用户信息")
    @GetMapping("page")
    public Result<IPage<UserInfo>> pageUserInfo(@RequestParam long current, @RequestParam long size, UserInfoQueryVo queryVo) {
        IPage<UserInfo> page = new Page<>(current, size);
        String phone = queryVo.getPhone();
        BaseStatus status = queryVo.getStatus();
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(phone != null, "phone", phone).eq(status != null, "status", status);
        IPage<UserInfo> userInfoPage = userInfoService.page(page, queryWrapper);
        return Result.ok(userInfoPage);
    }

    @Operation(summary = "根据用户id更新账号状态")
    @PostMapping("updateStatusById")
    public Result updateStatusById(@RequestParam Long id, @RequestParam BaseStatus status) {
        userInfoService.updateUserStatus(id, status);
        return Result.ok();
    }
}
